package com.xiangban.yanglaodajie_admin.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMqConfig {

    @Bean
    DirectExchange orderDirect() {
        return (DirectExchange) ExchangeBuilder
                .directExchange("order")
                .durable(true)
                .build();
    }

    @Bean
    DirectExchange orderTtlDirect() {
        return (DirectExchange) ExchangeBuilder
                .directExchange("ttl.order")
                .durable(true)
                .build();
    }

    @Bean
    public Queue orderQueue() {
        return new Queue("order.cancel");
    }

    @Bean
    public Queue orderTtlQueue() {
        return QueueBuilder
                .durable("delay.order.cancel")
                .withArgument("x-dead-letter-exchange", "order")
                .withArgument("x-dead-letter-routing-key", "order.routingKey")//到期后转发的路由键
                .build();
    }

    @Bean
    Binding orderBinding(DirectExchange orderDirect, Queue orderQueue){
        return BindingBuilder
                .bind(orderQueue)
                .to(orderDirect)
                .with("order.routingKey");
    }

    @Bean
    Binding orderTtlBinding(DirectExchange orderTtlDirect, Queue orderTtlQueue){
        return BindingBuilder
                .bind(orderTtlQueue)
                .to(orderTtlDirect)
                .with("ttl.order.routingKey");
    }

}
